var express = require('express');
var router = express.Router();
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server, { transports: ['websocket'] });

io.on('connection', function(socket){
    // console.log('一个用户已连接');

    socket.on("disconnect", function() {
        // console.log("一个用户已断开连接");
    });
    // socket.on("message", function(obj) {
    //   console.log(obj.test)
    //     //延迟3s返回信息给客户端
    //     setTimeout(function(){
    //         console.log('the websokcet message is'+obj);
    //         io.emit("message", obj);
    //     },3000);
    // });
    socket.on('sendMsg', (data) => {
    	console.log(data)
    	//给所有用户发送
        io.sockets.emit('setId', {id: '1234567'})
        //给当前用户发送
        //socket.emit('setId', { id: '123456' })
    })
    socket.on('sendNotice', (data) => {
    	console.log(data)
    	//给所有用户发送
        io.sockets.emit('getNotice', {data})
        //给当前用户发送
        //socket.emit('setId', { id: '123456' })
    })
    socket.on("error", function(obj) {
        console.log(obj)
        
      });
});
//开启端口监听socket
server.listen(3001);

router.get('/imRoom', function(req, res, next) {
    res.render('im/imRoom');
});


module.exports = router;